<?php
session_start();
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) 
{
$host="db.cs.dal.ca"; // Host name 
$username="tameem"; // Mysql username 
$password="B00614969"; // Mysql password 
$db_name="tameem"; // Database name 
$tbl_name="users"; // Table name 

$con = mysql_connect("db.cs.dal.ca","tameem","B00614969");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("tameem", $con);
$result = mysql_query("SELECT * FROM users");

if($_GET["action"]=="e")
{
	$sql="SELECT * FROM users WHERE id=".$_GET['id'];
	$result2 = mysql_query($sql);
	$row = mysql_fetch_array($result2);
}


//template
?>
<link rel="stylesheet" type="text/css" href="style.css" />
<div id="wrap">

<div id="header">
<h1><a href="#">IAPL</a></h1>
<h2>It`s A Prof`s Life</h2>
</div>

<div id="menu">
<ul>
	<li><a href="index.php">Home</a></li>
	<li><a href="data.php">Data</a></li>
	<li><a href="users.php">Users</a></li>
	<li><a href="settings.php">Settings</a></li>
	<li><a href="logout.php">Logout</a></li>
</ul>
</div>

<div id="content">
<div class="left"> 

<h2>Users</h2>
<?if ($_GET["action"]=="e"){?>
<p class="msg"> You are currently editing an existing entry</p>
<?php }?>

<form name="myform" action="<?if ($_GET["action"]!="e"){echo "edit_users.php?action=a" ;} else {echo "edit_users.php?action=e&id=".$_GET['id']; } ?>" method="POST">
	<div align="center">
	<table>
	<tr>
	<td>Name: </td>
			<td><input type="text" size="25" id="user" name="user" value="<?php if($_GET["action"]=="e"){ echo $row['id'];}?>"></td>
	<td>Password: </td>
			<td><input type="text" size="25" id="pass" name="pass" value="<?php if($_GET["action"]=="e"){ echo $row['password'];}?>"></td>
	<tr>
	<td>Role: </td>
			<td><input type="text" size="25" id="role" name="role" value="<?php if($_GET["action"]=="e"){ echo $row['role'];}?>"></td>
	<td><input type="submit" /></td>
	</tr>
	</table>
	</div>
</form>

</br>
<div align="center">
<table id="dtable">
	<tbody>
	<tr>
		<th>Username</th>
		<th>Role</th>
		<th>Start Date</th>
		<th>Edit/Delete</th>
	</tr>
	<?php
	$i=0;
	while($row = mysql_fetch_array($result))
	{
		if($i==0)
		{
		?>
			<tr class="r0">
		<?php
			$i=1;
		}
		else if($i==1)
		{
		?>
			<tr class="r1">
		<?php
			$i=0;
		}
		?>
			<td><?php echo $row['id']?></td>
			<td><?php echo $row['role']?></td>
			<td><?php echo $row['start']?></td>
			<td><a href="users.php?action=e&id=<?php echo $row['id']?>">Edit</a>/ <a href="edit_users.php?action=d&id=<?php echo $row['id']?>">Delete</a>
		</tr>
	<?php	
	}
	?>
	</tbody>
</table>
</div>
</div>


<div style="clear: both;"> </div>

</div>

<div id="bottom"> </div>
<div id="footer">
Designed by <a href="http://www.free-css-templates.com/">Free CSS Templates</a>, Thanks to <a href="http://www.openwebdesign.org/">web design company</a>
</div>

</div>


	
<?php
mysql_close($con);
	?>
<?php
}
else
{
	header("location:index.php");
}
?>